<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem :to="`/category/${category?.top.id}`">
      {{ category?.top.name }}
    </XtxBreadItem>
    <transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="category.sub.id">{{
        category.sub.name
      }}</XtxBreadItem>
    </transition>
  </XtxBread>
</template>
<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
export default {
  name: 'SubBread',
  setup () {
    const store = useStore()
    const route = useRoute()
    const category = computed(() => {
      const obj = {
        top: {},
        sub: {}
      }
      store.state.category.list.forEach(item => {
        item.children && item.children.forEach(sub => {
          if (sub.id === route.params.id) {
            obj.sub = sub
            obj.top = item
          }
        })
      })
      return obj
    })
    return {
      category
    }
  }
}
</script>
